//package com.etv.launcher;
//
//import android.content.Context;
//import android.content.Intent;
//import android.graphics.Bitmap;
//import android.os.Bundle;
//import android.graphics.drawable.Drawable;
//
//import androidx.annotation.NonNull;
//import androidx.annotation.Nullable;
//import androidx.leanback.app.DetailsSupportFragment;
//import androidx.leanback.app.DetailsSupportFragmentBackgroundController;
//import androidx.leanback.widget.Action;
//import androidx.leanback.widget.ArrayObjectAdapter;
//import androidx.leanback.widget.ClassPresenterSelector;
//import androidx.leanback.widget.DetailsOverviewRow;
//import androidx.leanback.widget.FullWidthDetailsOverviewRowPresenter;
//import androidx.leanback.widget.FullWidthDetailsOverviewSharedElementHelper;
//import androidx.leanback.widget.HeaderItem;
//import androidx.leanback.widget.ImageCardView;
//import androidx.leanback.widget.ListRow;
//import androidx.leanback.widget.ListRowPresenter;
//import androidx.leanback.widget.OnActionClickedListener;
//import androidx.leanback.widget.OnItemViewClickedListener;
//import androidx.leanback.widget.Presenter;
//import androidx.leanback.widget.Row;
//import androidx.leanback.widget.RowPresenter;
//import androidx.core.app.ActivityOptionsCompat;
//import androidx.core.content.ContextCompat;
//
//import android.util.Log;
//import android.widget.Toast;
//
//import com.bumptech.glide.Glide;
//import com.bumptech.glide.request.target.SimpleTarget;
//import com.bumptech.glide.request.transition.Transition;
//
//import java.util.Collections;
//import java.util.List;
//
///*
// * LeanbackDetailsFragment extends DetailsFragment, a Wrapper fragment for leanback details screens.
// * It shows a detailed view of video and its meta plus related videos.
// */
//public class VideoDetailsFragment extends DetailsSupportFragment {
//    private static final String TAG = "VideoDetailsFragment";
//
//    private static final int ACTION_WATCH_TRAILER = 1;
//    private static final int ACTION_RENT = 2;
//    private static final int ACTION_BUY = 3;
//
//    private static final int DETAIL_THUMB_WIDTH = 274;
//    private static final int DETAIL_THUMB_HEIGHT = 274;
//
//    private static final int NUM_COLS = 10;
//
//    private Movie mSelectedMovie;
//
//    private ArrayObjectAdapter mAdapter;
//    private ClassPresenterSelector mPresenterSelector;
//
//    private DetailsSupportFragmentBackgroundController mDetailsBackground;
//
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        Log.d(TAG, "onCreate DetailsFragment");
//        super.onCreate(savedInstanceState);
//
//        mDetailsBackground = new DetailsSupportFragmentBackgroundController(this);
//
//        mSelectedMovie =
//                (Movie) getActivity().getIntent().getSerializableExtra(DetailsActivity.MOVIE);
//        if (mSelectedMovie != null) {
//            mPresenterSelector = new ClassPresenterSelector();
//            mAdapter = new ArrayObjectAdapter(mPresenterSelector);
//            setupDetailsOverviewRow();
//            setupDetailsOverviewRowPresenter();
//            setupRelatedMovieListRow();
//            setAdapter(mAdapter);
//            initializeBackground(mSelectedMovie);
//            setOnItemViewClickedListener(new ItemViewClickedListener());
//        } else {
//            Intent intent = new Intent(getActivity(), MainActivity.class);
//            startActivity(intent);
//        }
//    }
//
//    private void initializeBackground(Movie data) {
//        mDetailsBackground.enableParallax();
//        Glide.with(getActivity())
//                .asBitmap()
//                .centerCrop()
//                .error(R.drawable.default_background)
//                .load(data.getBackgroundImageUrl())
//                .into(new SimpleTarget<Bitmap>() {
//                    @Override
//                    public void onResourceReady(@NonNull Bitmap bitmap,
//                                                @Nullable Transition<? super Bitmap> transition) {
//                        mDetailsBackground.setCoverBitmap(bitmap);
//                        mAdapter.notifyArrayItemRangeChanged(0, mAdapter.size());
//                    }
//                });
//    }
//
//    private void setupDetailsOverviewRow() {
//        Log.d(TAG, "doInBackground: " + mSelectedMovie.toString());
//        final DetailsOverviewRow row = new DetailsOverviewRow(mSelectedMovie);
//        row.setImageDrawable(
//                ContextCompat.getDrawable(getActivity(), R.drawable.default_background));
//        int width = convertDpToPixel(getActivity().getApplicationContext(), DETAIL_THUMB_WIDTH);
//        int height = convertDpToPixel(getActivity().getApplicationContext(), DETAIL_THUMB_HEIGHT);
//        Glide.with(getActivity())
//                .load(mSelectedMovie.getCardImageUrl())
//                .centerCrop()
//                .error(R.drawable.default_background)
//                .into(new SimpleTarget<Drawable>(width, height) {
//                    @Override
//                    public void onResourceReady(@NonNull Drawable drawable,
//                                                @Nullable Transition<? super Drawable> transition) {
//                        Log.d(TAG, "details overview card image url ready: " + drawable);
//                        row.setImageDrawable(drawable);
//                        mAdapter.notifyArrayItemRangeChanged(0, mAdapter.size());
//                    }
//                });
//
//        ArrayObjectAdapter actionAdapter = new ArrayObjectAdapter();
//
//        actionAdapter.add(
//                new Action(
//                        ACTION_WATCH_TRAILER,
//                        getResources().getString(R.string.watch_trailer_1),
//                        getResources().getString(R.string.watch_trailer_2)));
//        actionAdapter.add(
//                new Action(
//                        ACTION_RENT,
//                        getResources().getString(R.string.rent_1),
//                        getResources().getString(R.string.rent_2)));
//        actionAdapter.add(
//                new Action(
//                        ACTION_BUY,
//                        getResources().getString(R.string.buy_1),
//                        getResources().getString(R.string.buy_2)));
//        row.setActionsAdapter(actionAdapter);
//
//        mAdapter.add(row);
//    }
//
//    private void setupDetailsOverviewRowPresenter() {
//        // Set detail background.
//        FullWidthDetailsOverviewRowPresenter detailsPresenter =
//                new FullWidthDetailsOverviewRowPresenter(new DetailsDescriptionPresenter());
//        detailsPresenter.setBackgroundColor(
//                ContextCompat.getColor(getActivity(), R.color.selected_background));
//
//        // Hook up transition element.
//        FullWidthDetailsOverviewSharedElementHelper sharedElementHelper =
//                new FullWidthDetailsOverviewSharedElementHelper();
//        sharedElementHelper.setSharedElementEnterTransition(
//                getActivity(), DetailsActivity.SHARED_ELEMENT_NAME);
//        detailsPresenter.setListener(sharedElementHelper);
//        detailsPresenter.setParticipatingEntranceTransition(true);
//
//        detailsPresenter.setOnActionClickedListener(new OnActionClickedListener() {
//            @Override
//            public void onActionClicked(Action action) {
//                if (action.getId() == ACTION_WATCH_TRAILER) {
//                    Intent intent = new Intent(getActivity(), PlaybackActivity.class);
//                    intent.putExtra(DetailsActivity.MOVIE, mSelectedMovie);
//                    startActivity(intent);
//                } else {
//                    Toast.makeText(getActivity(), action.toString(), Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
//        mPresenterSelector.addClassPresenter(DetailsOverviewRow.class, detailsPresenter);
//    }
//
//    private void setupRelatedMovieListRow() {
//        String subcategories[] = {getString(R.string.related_movies)};
//        List<Movie> list = MovieList.getList();
//
//        Collections.shuffle(list);
//        ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter());
//        for (int j = 0; j < NUM_COLS; j++) {
//            listRowAdapter.add(list.get(j % 5));
//        }
//
//        HeaderItem header = new HeaderItem(0, subcategories[0]);
//        mAdapter.add(new ListRow(header, listRowAdapter));
//        mPresenterSelector.addClassPresenter(ListRow.class, new ListRowPresenter());
//    }
//
//    private int convertDpToPixel(Context context, int dp) {
//        float density = context.getResources().getDisplayMetrics().density;
//        return Math.round((float) dp * density);
//    }
//
//    private final class ItemViewClickedListener implements OnItemViewClickedListener {
//        @Override
//        public void onItemClicked(
//                Presenter.ViewHolder itemViewHolder,
//                Object item,
//                RowPresenter.ViewHolder rowViewHolder,
//                Row row) {
//
//            if (item instanceof Movie) {
//                Log.d(TAG, "Item: " + item.toString());
//                Intent intent = new Intent(getActivity(), DetailsActivity.class);
//                intent.putExtra(getResources().getString(R.string.movie), mSelectedMovie);
//
//                Bundle bundle =
//                        ActivityOptionsCompat.makeSceneTransitionAnimation(
//                                        getActivity(),
//                                        ((ImageCardView) itemViewHolder.view).getMainImageView(),
//                                        DetailsActivity.SHARED_ELEMENT_NAME)
//                                .toBundle();
//                getActivity().startActivity(intent, bundle);
//            }
//        }
//    }
//}